package client;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;

public class client {
    static Configuration conf = null;
    static Connection conn = null;


    static {
        //获取一个配置文件对象
        conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum", "linux121,linux122");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //通过conf获取到hbase集群的连接
        try {
            conn = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createTable() throws IOException {
        //获取HbaseAdmin对象用来创建表
        HBaseAdmin admin = (HBaseAdmin)conn.getAdmin();
        //创建Htabledesc描述器，表描述器
        final HTableDescriptor relation = new HTableDescriptor(TableName.valueOf("relation"));
        //指定列族
        relation.addFamily(new HColumnDescriptor("friends"));
        admin.createTable(relation);
        System.out.println("relation表创建成功！！");
        admin.close();
    }

    public void initDatas() throws IOException {
        //需要获取一个table对象
        final Table relation = conn.getTable(TableName.valueOf("relation"));

        //准备put对象
        final Put uid1 = new Put(Bytes.toBytes("uid1"));//指定rowkey

        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"), Bytes.toBytes("udi2"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid3"), Bytes.toBytes("udi3"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid4"), Bytes.toBytes("udi4"));

        final Put uid2 = new Put(Bytes.toBytes("uid2"));//指定rowkey
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("udi1"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid3"), Bytes.toBytes("udi3"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid4"), Bytes.toBytes("udi4"));


        //准备list<puts>,可以执行批量插入
        ArrayList<Put> puts = new ArrayList<Put>();

        puts.add(uid1);
        puts.add(uid2);

        //插入数据，参数类型是put
        relation.put(puts);

        //关闭table对象
        relation.close();
        System.out.println("插入数据到relation表成功！！");
    }

    //删除某个用户其中一个好友，

    public void deleteFriedns(String uid, String friend) throws IOException {
        final Table relation = conn.getTable(TableName.valueOf("relation"));
        final Delete delete = new Delete(Bytes.toBytes(uid));
        delete.addColumn(Bytes.toBytes("friends"), Bytes.toBytes(friend));//最好使用协处理器实现

        relation.delete(delete);

    }


    public static void main(String[] args) throws IOException {
        final client r = new client();
        //r.createTable();//创建表
        //r.initDatas();//初始化数据
        //假设uid1用户删除了uid2这个好友，需要执行删除操作
        r.deleteFriedns("uid1", "uid2");
    }
}
